package com.example.property.framgnet

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.common.mvi.BaseMviFragment
import com.example.path.RouterPath

import com.example.property.R
import com.example.property.adapter.CarApplyAdapter
import com.example.property.databinding.FragmentCarApplyBinding
import com.example.property.entity.CarApplyResultEntity
import com.example.property.entity.CarApplyValues
import com.example.property.intent.CarApplyViewIntent
import com.example.property.uistate.CarApplyUiState
import com.example.property.viewmodel.CarApplyViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class CarApplyFragment : BaseMviFragment<FragmentCarApplyBinding>() {

    private val mViewModel:CarApplyViewModel by activityViewModels()
    private lateinit var carApplyAdapter:CarApplyAdapter
    private var list = mutableListOf<CarApplyValues>()
    var sub=1

    companion object {
        @JvmStatic
        fun newInstance() = CarApplyFragment()
    }

    override fun getLayoutId(): Int =R.layout.fragment_car_apply

    override fun initViews() {

    }



    override suspend fun sendViewIntent() {
        mViewModel.channel.send(CarApplyViewIntent.LoadCarApplyList)
    }

    override suspend fun collectUIState() {
        mViewModel.uiState.collect{
            when(it){
                is CarApplyUiState.LoadCarApplySuccess->loadCarSuccess(it.data as MutableList<CarApplyValues>)
                is CarApplyUiState.LoadCarApplyFail->{showToastMessage("加载失败")}
                else->{}
            }
        }
    }

    private fun loadCarSuccess(carApplyValues: MutableList<CarApplyValues>) {
        list.clear()
        for (i in 0..carApplyValues.size-1){
            if (i%5==0){
                list.add(CarApplyValues(0,-1,"","第${sub++}行","","",true))
            }
            list.add(carApplyValues[i])
        }
        carApplyAdapter= CarApplyAdapter(list)
        mDataBinding.rvCar.adapter=carApplyAdapter
        mDataBinding.rvCar.layoutManager=GridLayoutManager(requireContext(),6)
        carApplyAdapter.setOnItemClickListener(object :CarApplyAdapter.setOnItemClickListener{
            override fun onItemClick(view: View, position: Int) {
                var entity = list[position]
                if (entity.parkUser>0){
                    ARouter.getInstance()
                        .build(RouterPath.PATH_DETAIL)
                        .withSerializable("carApplyValues",entity)
                        .navigation()
                }else{
                    showToastMessage("当前车位课申请")
                }
            }
        })

    }



}